package com.cirrusblue.caching.roo.addon;

import java.util.logging.Logger;

import org.springframework.roo.model.JavaType;
import org.springframework.roo.shell.CliAvailabilityIndicator;
import org.springframework.roo.shell.CliCommand;
import org.springframework.roo.shell.CliOption;
import org.springframework.roo.shell.CommandMarker;
import org.springframework.roo.shell.converters.StaticFieldConverter;
import org.springframework.roo.support.lifecycle.ScopeDevelopmentShell;
import org.springframework.roo.support.util.Assert;

/**
 * Sample of a command class. The command class is registered by the Roo shell following an
 * automatic classpath scan. You can provide simple user presentation-related logic in this
 * class. You can return any objects from each method, or use the logger directly if you'd
 * like to emit messages of different severity (and therefore different colours on 
 * non-Windows systems).
 * 
 */
@ScopeDevelopmentShell
public class CachingCommands implements CommandMarker {
	
	private static Logger logger = Logger.getLogger(CachingCommands.class.getName());

	private CachingOperations operations;

	public CachingCommands(StaticFieldConverter staticFieldConverter, CachingOperations operations) {
		Assert.notNull(staticFieldConverter, "Static field converter required");
		Assert.notNull(operations, "Operations object required");
		staticFieldConverter.add(CacheProvider.class);
		this.operations = operations;
		logger.warning("Loaded " + CachingCommands.class.getName() + "; try the 'welcome' commands");
	}
	
	@CliAvailabilityIndicator("cache setup")
	public boolean isPropertyAvailable() {
		return operations.isCachingAvailable();
	}
	
	@CliCommand(value="cache setup", help="Configures a caching setup for a given type")
	public void configureCache(@CliOption(key="class", mandatory=false, optionContext="update,project", unspecifiedDefaultValue="*", help="The name of the entity object which the controller exposes to the web tier") JavaType type) {	
		operations.configureCaching(type);
	}

}